package com.tcat.item.web;


import com.tcat.item.bo.CategoryBo;
import com.tcat.item.pojo.Category;
import com.tcat.item.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Api(tags = "分类管理")
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父节点id查询商品分类         解决
     * 请求方式：Get
     * 请求路径：/api/item/category/list。其中/api是网关前缀，/item是网关的路由映射，真实的路径应该是/category/list
     * 请求参数：pid=0，根据tree组件的说明，应该是父节点的id，第一次查询为0，那就是查询一级类目
     * 返回结果：根据tree组件的用法 可知，返回的应该是json数组：
     * [
     *   {
     *       "id":74,
     *       "name":"手机",
     *       "parentId":0,
     *       "isParent":true
     *       "sort":2
     *   },
     *   {
     *       "id":75,
     *       "name":"家用电器",
     *       "parentId":0,
     *       "isParent":true,
     *       "sort":3
     *   }
     * ]
     * @param pid
     * @return
     */
    @ApiOperation(value = "根据父节点id查询商品分类")
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoryListByPid(@RequestParam("pid") Long pid){
        //return ResponseEntity.ok(categoryService.queryCategoryListByPid(pid));
        //如果pid的值为-1则需要 获取数据库中最后一条数据
        if(pid == -1){
            List<Category> last=categoryService.queryLast();
            return ResponseEntity.ok(last);
        }else{
            List<Category> list = categoryService.queryCategoryListByPid(pid);
            if(list == null){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(list);
        }
    }




    /**
     * 保存（新增分类）            解决
     * @param category
     * @return
     */
    @ApiOperation(value = "新增分类")
    @PostMapping
    public ResponseEntity<Void> saveCategory(Category category){
        categoryService.saveCategory(category);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 更新            解决
     * @param category
     * @return
     */
    @ApiOperation(value = "更新分类")
    @PutMapping
    public ResponseEntity<Void> updateCategory(Category category){
        categoryService.updateCategroy(category);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }

    /**
     * 删除（删除分类 ）     解决
     * @param id
     * @return
     */
    @ApiOperation(value = "删除分类")
    @DeleteMapping("cid/{cid}")
    public ResponseEntity<Void> deleteCategory(@PathVariable("cid") Long id){
        categoryService.deleteCategory(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }




//---------------------------


    /**
     * （MyBrand中）用于修改品牌信息时，商品分类信息的回显
     * @param bid
     * @return
     */
    @ApiOperation(value = "用于修改品牌信息时，商品分类信息的回显")
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid){
        List<Category> list=categoryService.queryByBrandId(bid);
        if(list == null || list.size()<1){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(list);
    }




    /**
     * 根据id查询商品分类
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据id查询商品分类")
    @GetMapping("list/ids")
    public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(categoryService.queryByIdS(ids));
    }

    /**
     * 根据三级分类id，查询1-3级的分类
     * @param id
     * @return
     */
    @ApiOperation(value = "根据三级分类id，查询1-3级的分类")
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id")Long id){
        return ResponseEntity.ok(categoryService.queryAllByCid3(id));
    }





}
